
// 导入静态/动态路由
import { asyncRoutes, constantRoutes } from '@/router/index.js';
// 这个模块是用来存放当前的静态路由和当前用户的动态路由的
const state = {
  routes: constantRoutes, // 所有人默认拥有的静态路由的权限
};
const mutations = {
  setRoutes(state, newRoutes) {
    // 将静态路由和新增的动态路由合并
    // 一下这种写是在前一个登录用户的基础之上，添加的路由模块，业务不正确
    // state.routes = [...state.routes, ...newRoutes];
    state.routes = [...constantRoutes, ...newRoutes];
  },
};

const actions = {
  filterRoutes(context, menus) {
    const routes = [];
    menus.forEach(key => {
      // 注意：asyncRoutes 是一个数组对象
      routes.push(...asyncRoutes.filter(item => item.name === key));
    });
    context.commit('setRoutes', routes);
    return routes;
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
;
